package com.ruoyi.repayment.service;


import com.ruoyi.repayment.cofig.AverageCapitalPlusInterestUtils;
import com.ruoyi.repayment.mapper.LoanMapper;
import com.ruoyi.repayment.mapper.SuchratesMapper;
import com.ruoyi.repayment.pojo.Benjin;
import com.ruoyi.repayment.pojo.Product;
import com.ruoyi.repayment.pojo.suchrates;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SuchratesService {
    @Autowired
    SuchratesMapper suchratesMapper;

    @Autowired
    LoanMapper loanMapper;

    @Autowired
    AverageCapitalPlusInterestUtils averageCapitalPlusInterestUtils;

    /**
     *
     *
     * 等额本息计算：
     * 每月应还款额＝借款本金×月利率×（1＋月利率）^还款月数/[（1＋月利率）^还款月数－1]；
     * 总还款额=每期还款额*还款月数。
     * @param loansprice
     */

    public void calculate(Integer id, Integer productId,Integer userId) {
        suchrates suchrates = new suchrates();
        Product list=loanMapper.findByList(productId);
        Integer totalmonth=list.getPeriods();
        double loansprice=list.getMaxquota();
        double yearRate=list.getInterestRate();
        suchrates.setApplyforId(id);
        suchrates.setUserId(userId);
        for (int i = 1; i <totalmonth+1; i++) {
            suchrates.setApplyforId(id);
            suchrates.setMonths(i);
            double monthRate = yearRate / 12;

                BigDecimal monthInterest;
                BigDecimal multiply = new BigDecimal(loansprice).multiply(new BigDecimal(monthRate));
                BigDecimal sub = new BigDecimal(Math.pow(1 + monthRate, totalmonth))
                        .subtract(new BigDecimal(Math.pow(1 + monthRate, i - 1)));
                monthInterest = multiply.multiply(sub).divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 2,
                        BigDecimal.ROUND_DOWN);
                monthInterest = monthInterest.setScale(2, BigDecimal.ROUND_DOWN);

                double rs = monthInterest.doubleValue();
                suchrates.setInterests(rs);

            //每月偿还本金
            BigDecimal monthIncome = new BigDecimal(loansprice)
                    .multiply(new BigDecimal(monthRate * Math.pow(1 + monthRate, totalmonth)))
                    .divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 2, BigDecimal.ROUND_DOWN);
            Map<Integer, BigDecimal> mapInterest = averageCapitalPlusInterestUtils.getPerMonthInterest(loansprice, yearRate, totalmonth);
            Map<Integer, BigDecimal> mapPrincipal = new HashMap<Integer, BigDecimal>();

            for (Map.Entry<Integer, BigDecimal> entry : mapInterest.entrySet()) {
                mapPrincipal.put(entry.getKey(), monthIncome.subtract(entry.getValue()));
            }
            suchrates.setPrincipal(mapPrincipal.get(i).doubleValue());
            double interestCount = averageCapitalPlusInterestUtils.getInterestCount(loansprice, yearRate, totalmonth);
            suchrates.setInterestCount(interestCount);
            double principalInterestCount = averageCapitalPlusInterestUtils.getPrincipalInterestCount(loansprice, yearRate, totalmonth);
            if(suchrates.getFigure()==0.00){
                suchrates.setFigure(loansprice-suchrates.getPrincipal());
            }else {

                suchrates.setFigure(suchrates.getFigure()-suchrates.getPrincipal());
                if (suchrates.getFigure()<0){
                    suchrates.setFigure(0.00);
                }
            }
            suchrates.setMents(suchrates.getPrincipal()+suchrates.getInterests());
            suchrates.setZprice(principalInterestCount);
            suchratesMapper.insertinto(suchrates);
        }

    }


    public List<Benjin> Calculatexq(Integer id, Integer userId) {
        List<Benjin> list=suchratesMapper.Calculatexq(id,userId);
        return list;
    }
}
